<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta
      name="viewport"
      content="width=
    , initial-scale=1.0"
    />
    <title>Document</title>
  </head>

  <body>
    <script>
      const arr = [
        { name: '广东', id: '1', pid: '' },
        { name: '湖南', id: '2', pid: '' },
        { name: '湖北', id: '2', pid: '' },
        { name: '深圳', id: '1-1', pid: '1' },
        { name: '广州', id: '1-2', pid: '1' },
        { name: '东莞', id: '1-3', pid: '1' },
        { name: '南山区', id: '1-1-1', pid: '1-1' },
        { name: '宝安区', id: '1-1-2', pid: '1-1' },
        { name: '石岩', id: '1-1-2-1', pid: '1-1-2' },
        { name: '创维', id: '1-1-2-1-1', pid: '1-1-2-1' },
        { name: '虎门', id: '1-3-1', pid: '1-3' }
      ]

      function Tree (arr) {
        let info = arr.reduce(
          (tree, data) => ((tree[data.id] = data), (data.children = []), tree),
          {}
        )
        return arr.filter(data => {
          info[data.pid] && info[data.pid].children.push(data)
          return !data.pid
        })
      }
     Tree(arr)
      console.log(arr);

      // 写一个方法将arr数据转换成result数据
    </script>
  </body>
</html>
